package com.ruix.app.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.ruix.app.R;

/**
 * Created by hadexs on 2017/6/18.
 */

public class VoiceProgressView extends View {

    private int progressColor;
    private float progressWidth;
    private float sweepAngle;
    private float startAngle;
    private Paint paint;
//    private int centre;
    private int maxProgress = 60;
    private RectF oval;

    public VoiceProgressView(Context context) {
        super(context);
        init(context);
    }

    public VoiceProgressView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public VoiceProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
//        progressColor
        init(context);
    }


    private void init(Context context){
        progressWidth = context.getResources().getDimension(R.dimen.text_size_8);
        startAngle = -90;
        initPaint();

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (oval == null) {
            int width = getWidth();
            float other = progressWidth / 2;
            oval = new RectF(0 + other, 0 + other, width - other, width - other);  //用于定义的圆弧的形状和大小的界限
        }
        canvas.drawArc(oval, startAngle, sweepAngle, false, paint);
    }


    private void initPaint(){
        paint = new Paint();
        paint.setStyle(Paint.Style.STROKE); //设置空心
        paint.setStrokeWidth(progressWidth); //设置圆环的宽度
        paint.setAntiAlias(true);  //消除锯齿
//        paint.setAlpha(178);
    }


    public void setMaxProgress(int max) {
        this.maxProgress = max;
    }


    public void setProgressColor(int progressColor) {
        this.progressColor = progressColor;
        paint.setColor(progressColor);
    }

    public void setProgress(long progress){
        if(progress <= maxProgress){
            sweepAngle = 360 * progress / maxProgress ;
            postInvalidate();
        }
    }





}
